# Maintainer: Rodrigo Hernandez <kwizatz@aeongames.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=v8
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.3.1
pkgrel=1
pkgdesc="Fast and modern Javascript engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://v8.dev"
license=('spdx:BSD-3-Clause')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-pkg-config"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-gn"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "git")
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-icu")
options=('strip')
source=("https://github.com/v8/v8/archive/refs/tags/${pkgver}.tar.gz"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=32324ec2c551579f3b226a23a7a05d8545998399"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=d866d41e168ec04545bb17031fa911007a0581f4"
        "bare-clones/common::git+https://chromium.googlesource.com/chromium/src/base/trace_event/common.git#commit=521ac34ebd795939c7e16b37d9d3ddb40e8ed556"
        "bare-clones/googletest::git+https://chromium.googlesource.com/external/github.com/google/googletest.git#commit=a16bfcfda1ea994c1abec23cca8f530953042dfa"
        "bare-clones/jinja2::git+https://chromium.googlesource.com/chromium/src/third_party/jinja2.git#commit=4633bf431193690c3491244f5a0acbe9ac776233"
        "bare-clones/markupsafe::git+https://chromium.googlesource.com/chromium/src/third_party/markupsafe.git#commit=13f4e8c9e206567eeb13bf585406ddc574005748"
        "bare-clones/clang::git+https://chromium.googlesource.com/chromium/src/tools/clang.git#commit=1b8faee860dd0c549bf5070ff54a410c0370d075"
        "001-add-mingw-toolchain.patch"
        "002-buildflags-fixes.patch"
        "003-fix-macros-and-functions.patch"
        "004-fix-static-assert-implementations.patch"
        "005-fix-conflicting-macros.patch"
        "006-support-clang-in-mingw-mode.patch"
        "007-snapshot-use-system-zlib-header.patch"
        "008-prioritized-native-thread-on-windows.patch"
        "009-unicode-for-wide-char-functions.patch"
        "010-disable-msvc-hack.patch"
        "011-make-sure-that-__rdtsc-is-declared.patch"
        "012-remove-dllimport-attributes.patch"
        "013-builtin-deps-fixes.patch"
        "icu.gn"
        "zlib.gn"
        "v8_libbase.pc"
        "v8_libplatform.pc"
        "v8.pc")
sha256sums=('ff5c9974a01f51cea6f3e4604875a6131d3321c8a814f49eb91b988d71da6cb1'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'e0045d647c7719954bda71547374a4c8a4beaa7ed2f9ed54e885edd44c928701'
            'a6c78f73a0acba3e7505a5cb40e7131c8a79084459d31c01019c823c77278dc4'
            '399f5e88333d1e32c86236b8fe549ae25a72e2bde91ec138c8e93fbcd578130a'
            '3a609e6f846b147f17707117faf9fc32a633c29f60cc6413cbf1258b0d14ebe5'
            '420e7cd413dee1f046b7d386acb3ef4c549e28e7198710cccd3d978e7b944065'
            '98fd938e788ce5a817f7106ea7a97918e77de19482da0bbedfbded786b79af55'
            '9967887ffdb90c0b0d5cb9299f01d2f763db0ad224d8c0458d9d359cab18d535'
            '0ef0821ecc19e529743e83d4df074f9de43c8086a6fa73265893fd78c04853d5'
            'a1bc97efe87f5a7a403cfc0bc720b1601a615bef73a63cb4c5bbabafb4d2a28d'
            '8183ba137645f31b0e2d0acf14511b28d2613221a4c402ad86ef5c9148bfd036'
            '6243b0725c504e5ce2156c2232c3ee460ad80b201f0b3250c401a7917e0a0bd4'
            '7a3752c019836dad415cd7e793f3edad9aa5f31743bf2a0b03b9ea21996169f6'
            '38a952d4069440b447fc63ebd0df6ad03889f03b253cac43c48eaebde29c8d7a'
            '5e3544f2d89ee344b333961d7ab4bc9f9257d4fa9c90375aa2fa06d5db1332db'
            'b99874ea904724911528a5530c14f3051c358805bd36df9d205df31458ebf8b9'
            'fbddbe4849b623afb320472aab316c9a08b54182161b6b4911cb7ef7d65e2b0e'
            'ca81528892f0e02fe7ecd159568610221fb8d0df109d9d0dbdfa00c1284dd0fc'
            '579e199a798ff76b72a5b17380a3cd09d361cb7cee99403090c3a266d1b2e527')

prepare() {
  echo "Patching build directory"
  cd ${srcdir}/build
  patch -p1 -i ${srcdir}/001-add-mingw-toolchain.patch
  python ${srcdir}/build/util/lastchange.py -o ${srcdir}/build/util/LASTCHANGE
  rm -rf ${srcdir}/${_realname}-${pkgver}/build
  ln -sf ${srcdir}/build ${srcdir}/${_realname}-${pkgver}
  echo "checkout_google_benchmark = false" > ${srcdir}/${_realname}-${pkgver}/build/config/gclient_args.gni

  echo "Patching zlib directory"
  mkdir -p ${srcdir}/${_realname}-${pkgver}/third_party/zlib
  ln -sf ${srcdir}/zlib.gn ${srcdir}/${_realname}-${pkgver}/third_party/zlib/BUILD.gn
  rm -rf ${srcdir}/${_realname}-${pkgver}/third_party/zlib/google
  ln -sf ${srcdir}/zlib/google ${srcdir}/${_realname}-${pkgver}/third_party/zlib

  echo "Patching icu directory"
  mkdir -p ${srcdir}/${_realname}-${pkgver}/third_party/icu
  ln -sf ${srcdir}/icu.gn ${srcdir}/${_realname}-${pkgver}/third_party/icu/BUILD.gn

  echo "Soft linking extra repos"
  mkdir -p ${srcdir}/${_realname}-${pkgver}/base/trace_event
  ln -sf ${srcdir}/common ${srcdir}/${_realname}-${pkgver}/base/trace_event
  mkdir -p ${srcdir}/${_realname}-${pkgver}/third_party/googletest/src
  ln -sf ${srcdir}/googletest/* ${srcdir}/${_realname}-${pkgver}/third_party/googletest/src
  ln -sf ${srcdir}/jinja2 ${srcdir}/${_realname}-${pkgver}/third_party
  ln -sf ${srcdir}/markupsafe ${srcdir}/${_realname}-${pkgver}/third_party
  ln -sf ${srcdir}/clang ${srcdir}/${_realname}-${pkgver}/tools

  echo "Patching v8 source"
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-buildflags-fixes.patch
  patch -p1 -i ${srcdir}/003-fix-macros-and-functions.patch
  patch -p1 -i ${srcdir}/004-fix-static-assert-implementations.patch
  patch -p1 -i ${srcdir}/005-fix-conflicting-macros.patch
  patch -p1 -i ${srcdir}/006-support-clang-in-mingw-mode.patch
  patch -p1 -i ${srcdir}/007-snapshot-use-system-zlib-header.patch
  patch -p1 -i ${srcdir}/008-prioritized-native-thread-on-windows.patch
  patch -p1 -i ${srcdir}/009-unicode-for-wide-char-functions.patch
  patch -p1 -i ${srcdir}/010-disable-msvc-hack.patch
  patch -p1 -i ${srcdir}/011-make-sure-that-__rdtsc-is-declared.patch
  patch -p1 -i ${srcdir}/012-remove-dllimport-attributes.patch
  patch -p1 -i ${srcdir}/013-builtin-deps-fixes.patch

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "${srcdir}/v8_libbase.pc"

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "${srcdir}/v8_libplatform.pc"

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "${srcdir}/v8.pc"
}

build() {
  cd ${_realname}-${pkgver}

  local _target=Release
  local _debug=false
  if check_option "debug" "y"; then
    _target=Debug
    _debug=true
  fi

  local _arch=x64
  if [[ ${CARCH} == i686 ]]; then
    _arch=x86
  fi

  if [[ ${MSYSTEM} == CLANG* ]]; then
    export CXX=clang++
    export CC=clang
    export LD=clang++
    export AR=llvm-ar
  else
    export CXX=g++
    export CC=gcc
    export LD=g++
    export AR=ar
  fi

  # generate build files
  ${MINGW_PREFIX}/bin/gn gen out/${_target}-${MSYSTEM} --args="
    target_cpu=\"${_arch}\"
    is_debug=${_debug}
    is_component_build=true
    use_custom_libcxx=false
    use_sysroot=false
    v8_enable_verify_heap=false
    v8_enable_system_instrumentation=false
    icu_use_data_file=false
    enable_iterator_debugging=${_debug}
    treat_warnings_as_errors=false"

  ${MINGW_PREFIX}/bin/ninja -C out/${_target}-${MSYSTEM} v8_libbase v8_libplatform v8 d8 torque torque-language-server mksnapshot
}

package() {
  cd ${_realname}-${pkgver}

  local _target=Release
  if check_option "debug" "y"; then
    _target=Debug
  fi

  install -d ${pkgdir}${MINGW_PREFIX}/{bin,include/{cppgc/internal,libplatform},lib/pkgconfig,share/licenses/${_realname}}
  install -Dm755 out/${_target}-${MSYSTEM}/d8.exe                      ${pkgdir}${MINGW_PREFIX}/bin/d8.exe
  install -Dm755 out/${_target}-${MSYSTEM}/torque.exe                  ${pkgdir}${MINGW_PREFIX}/bin/torque.exe
  install -Dm755 out/${_target}-${MSYSTEM}/torque-language-server.exe  ${pkgdir}${MINGW_PREFIX}/bin/torque-language-server.exe
  install -Dm755 out/${_target}-${MSYSTEM}/mksnapshot.exe              ${pkgdir}${MINGW_PREFIX}/bin/mksnapshot.exe
  install -Dm755 out/${_target}-${MSYSTEM}/*.dll                       ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm644 out/${_target}-${MSYSTEM}/snapshot_blob.bin           ${pkgdir}${MINGW_PREFIX}/bin/snapshot_blob.bin
  install -Dm755 out/${_target}-${MSYSTEM}/*.a                         ${pkgdir}${MINGW_PREFIX}/lib/
  install -Dm644 $srcdir/*.pc                                          ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/
  install -Dm644 include/*.h                                           ${pkgdir}${MINGW_PREFIX}/include/
  install -Dm644 include/cppgc/*.h                                     ${pkgdir}${MINGW_PREFIX}/include/cppgc/
  install -Dm644 include/cppgc/internal/*.h                            ${pkgdir}${MINGW_PREFIX}/include/cppgc/internal/
  install -Dm644 include/libplatform/*.h                               ${pkgdir}${MINGW_PREFIX}/include/libplatform/
  install -Dm644 LICENSE*                                              ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/
}
